# -*- coding:utf8 -*-
import time
#import os
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction


desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'Pixel 2 API 23'
desired_caps['newCommandTimeout'] = 8000
# desired_caps['automationName'] = 'UIAutomator2'
desired_caps['noReset'] = True
#desired_caps['app'] = '/Users/nju/Desktop/apk/apk/Keev1.apk'
desired_caps['appPackage'] = 'com.android.keepass'
desired_caps['appActivity']= '.KeePass'
desired_caps["unicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True
#desired_caps['appWaitActivity'] = 'com.keepassdroid.fileselect.FileSelectActivity'


#driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)
time.sleep(5)

# test case1: 开始页面,捐赠
el = driver.find_element_by_id("com.android.keepass:id/menu_donate")
el.click()

time.sleep(5)

driver.back()
# test case2: 开始页面，关于
el = driver.find_element_by_id("com.android.keepass:id/menu_about")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/about_button")
el.click()

time.sleep(1)
# test case3: 开始页面，设置，取消所有勾选，剪切板超时设置为30秒，应用程序超时设置为30秒
el = driver.find_element_by_accessibility_id("Settings")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[3]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_element_by_id("android:id/button2")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_element_by_id("android:id/button2")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_elements_by_id("android:id/text1")[0]
el.click()

time.sleep(5)

driver.back()

time.sleep(5)

driver.back()
# test case4: 开始页面，设置，恢复所有勾选，剪切板超时设置为1分钟，应用程序超时设置为1分钟
el = driver.find_element_by_accessibility_id("Settings")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[3]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[1]
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_elements_by_id("android:id/text1")[1]
el.click()

driver.back()

time.sleep(5)

driver.back()
# test case5: 开始页面，设置，恢复所有勾选，剪切板超时设置为5分钟，应用程序超时设置为5分钟
el = driver.find_element_by_accessibility_id("Settings")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

time.sleep(1)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[2]
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_elements_by_id("android:id/text1")[2]
el.click()

driver.back()

time.sleep(1)

driver.back()
# test case6: 开始页面，选择文件
el = driver.find_element_by_id("com.android.keepass:id/browse_button")
el.click()
time.sleep(5)
driver.back()

time.sleep(1)
#os.system("adb shell rm /storage/emulated/0/keepass/k.kdbx")
# test case7: 开始页面，创建，输入密码（一旦点击创建，便会产生文件夹）
el = driver.find_element_by_id("com.android.keepass:id/file_filename")
el.clear()

time.sleep(5)

el = driver.find_element_by_id("com.android.keepass:id/file_filename")
el.send_keys("/storage/emulated/0/keepass/k.kdbx")

el = driver.find_element_by_id("com.android.keepass:id/create")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/pass_password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/pass_conf_password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(2)

driver.back()

time.sleep(2)

driver.back()
# test case8: 开始页面，打开，选择该数据库作为默认数据库，然后首先输入错误密码，再输入正确密码进入
el = driver.find_elements_by_id("com.android.keepass:id/file_filename")[1]
el.clear()

time.sleep(5)

el = driver.find_elements_by_id("com.android.keepass:id/file_filename")[1]
el.send_keys("/storage/emulated/0/keepass/k.kdbx")

el = driver.find_element_by_id("com.android.keepass:id/open")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/default_database")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/password")
el.send_keys("1")

el = driver.find_element_by_id("com.android.keepass:id/pass_ok")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/pass_ok")
el.click()

time.sleep(5)
# test case9: 进入后主页面，添加群组
el = driver.find_element_by_id("com.android.keepass:id/add_group")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/cancel")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/add_group")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/group_name")
el.send_keys("TestGroup1")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(5)

# test case10: 进入后主页面, 添加条目
el = driver.find_element_by_id("com.android.keepass:id/add_entry")
el.click()
#driver.hide_keyboard()

el = driver.find_element_by_id("com.android.keepass:id/entry_cancel")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/add_entry")
el.click()
#driver.hide_keyboard()

el = driver.find_element_by_id("com.android.keepass:id/entry_title")
el.send_keys("TestEntry1")

el = driver.find_element_by_id("com.android.keepass:id/entry_user_name")
el.send_keys("User1")

el = driver.find_element_by_id("com.android.keepass:id/entry_url")
el.send_keys("www.amazon.com")

el = driver.find_element_by_id("com.android.keepass:id/entry_password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/entry_confpassword")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/entry_comment")
el.send_keys("Note")

el = driver.find_element_by_id("com.android.keepass:id/add_advanced")
el.click()

el = driver.find_elements_by_id("com.android.keepass:id/title")[0]
el.send_keys("TestEntry1")

el = driver.find_elements_by_id("com.android.keepass:id/protection")[0]
el.click()

el = driver.find_elements_by_id("com.android.keepass:id/value")[0]
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/entry_save")
el.click()

# test case11: 进入第一个群组，并添加群组
el = driver.find_elements_by_id("com.android.keepass:id/group_text")[0]
el.click()
time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/add_group")
el.click()
time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/group_name")
el.send_keys("TestGroup2")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(5)

# test case12: 在当前群组下，添加条目，使用生成的密码作为密码
el = driver.find_element_by_id("com.android.keepass:id/add_entry")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/entry_title")
el.send_keys("TestEntry2")

el = driver.find_element_by_id("com.android.keepass:id/entry_user_name")
el.send_keys("User2")

el = driver.find_element_by_id("com.android.keepass:id/entry_url")
el.send_keys("www.amazon.com")

el = driver.find_element_by_id("com.android.keepass:id/generate_button")
el.click()

time.sleep(1)

el = driver.find_elements_by_class_name("android.widget.CheckBox")[3]
el.click()

el = driver.find_elements_by_class_name("android.widget.CheckBox")[4]
el.click()

el = driver.find_elements_by_class_name("android.widget.CheckBox")[5]
el.click()

el = driver.find_elements_by_class_name("android.widget.CheckBox")[6]
el.click()

el = driver.find_elements_by_class_name("android.widget.CheckBox")[7]
el.click()

el = driver.find_element_by_id("com.android.keepass:id/btn_length12")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/generate_password_button")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/btn_length16")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/generate_password_button")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/btn_length8")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/generate_password_button")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/btn_length6")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/generate_password_button")
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/accept_button")
el.click()


el = driver.find_element_by_id("com.android.keepass:id/entry_comment")
el.send_keys("Note")


el = driver.find_element_by_id("com.android.keepass:id/entry_save")
el.click()

# test case13: 返回第一个群组，进行搜索，分别输入条目名称，说明，不存在的条目名称，不存在的条目说明进行搜索，
time.sleep(2)

driver.back()

el = driver.find_element_by_id("com.android.keepass:id/menu_search")
el.click()

el = driver.find_element_by_id("android:id/search_src_text")
el.send_keys("TestEntry")

driver.press_keycode(66)

time.sleep(5)

driver.back()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/menu_search")
el.click()

el = driver.find_element_by_id("android:id/search_src_text")
el.send_keys("Note")
driver.press_keycode(66)

time.sleep(5)

driver.back()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/menu_search")
el.click()

el = driver.find_element_by_id("android:id/search_src_text")
el.send_keys("qqqq")
driver.press_keycode(66)

time.sleep(5)

driver.back()

time.sleep(1)

# test case14: 进入主页下的第一个条目，点击隐藏/显示密码，然后点击网址进行访问
el = driver.find_elements_by_id("com.android.keepass:id/entry_text")[0]
el.click()

el = driver.find_element_by_id("com.android.keepass:id/menu_toggle_pass")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/menu_toggle_pass")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/entry_url")
el.click()

time.sleep(5)

driver.back()

time.sleep(5)

# test case15: 当前条目下，更多选项下各功能按钮
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

time.sleep(1)

driver.back()
time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

time.sleep(5)

driver.back()
time.sleep(2)

# test case16: 当前条目下，编辑当前条目，返回主页面
el = driver.find_element_by_id("com.android.keepass:id/entry_edit")
el.click()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/entry_save")
el.click()

time.sleep(5)

driver.back()

# test case17: 主页面下，更多选项，数据库的排序顺序
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

time.sleep(2)

# test case18: 主页面下，更多选项，改变主密钥
el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(5)

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/pass_password")
el.send_keys("00")

el = driver.find_element_by_id("com.android.keepass:id/pass_conf_password")
el.send_keys("00")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(1)

# test case19: 主页面下，锁定当前数据库，再次重新进入，然后退出(todo)
el = driver.find_element_by_id("com.android.keepass:id/menu_lock")
el.click()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/password")
el.send_keys("00")

el = driver.find_element_by_id("com.android.keepass:id/pass_ok")
el.click()

time.sleep(5)

driver.back()

time.sleep(5)

driver.back()

time.sleep(5)

os.system("adb shell rm /storage/emulated/0/keepass/keepass.kdb")
# test case20: 创建一个后缀为.kdb的数据库
el = driver.find_elements_by_id("com.android.keepass:id/file_filename")[1]
el.clear()

el = driver.find_elements_by_id("com.android.keepass:id/file_filename")[1]
el.send_keys("/storage/emulated/0/keepass/keepass.kdb")

el = driver.find_element_by_id("com.android.keepass:id/create")
el.click()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/pass_password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/pass_conf_password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(1)

# test case21: 当前数据库下，添加群组
el = driver.find_element_by_id("com.android.keepass:id/add_group")
el.click()
time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/group_name")
el.send_keys("TestGroup3")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(5)

# test case22: 进入当前数据库的一个群组（eMail），添加群组，选择新的图标作为群组图标
el = driver.find_elements_by_id("com.android.keepass:id/group_text")[0]
el.click()

time.sleep(2)

el = driver.find_element_by_id("com.android.keepass:id/add_group")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/icon_button")
el.click()

el = driver.find_elements_by_id("com.android.keepass:id/icon_text")[3]
el.click()

el = driver.find_element_by_id("com.android.keepass:id/group_name")
el.send_keys("TestGroup4")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(5)

# test case23: 当前群组下，添加条目
el = driver.find_element_by_id("com.android.keepass:id/add_entry")
el.click()

el = driver.find_element_by_id("com.android.keepass:id/entry_title")
el.send_keys("TestEntry3")

el = driver.find_element_by_id("com.android.keepass:id/entry_user_name")
el.send_keys("User3")

el = driver.find_element_by_id("com.android.keepass:id/entry_url")
el.send_keys("www.amazon.com")

el = driver.find_element_by_id("com.android.keepass:id/entry_password")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/entry_confpassword")
el.send_keys("0")

el = driver.find_element_by_id("com.android.keepass:id/entry_comment")
el.send_keys("Note")

el = driver.find_element_by_id("com.android.keepass:id/entry_save")
el.click()

time.sleep(5)

# test case24: 当前群组下，更多选项，数据库的排序顺序
el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

time.sleep(1)

# test case25: 当前群组下，更多选项，设置，数据库设置，更改加密次数
el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_element_by_id("com.android.keepass:id/rounds")
el.send_keys("400")

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(1)

driver.back()

time.sleep(1)

driver.back()

# test case26: 当前群组下，更多选项，改变主密钥
el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(5)

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

time.sleep(1)

el = driver.find_element_by_id("com.android.keepass:id/pass_password")
el.send_keys("00")

el = driver.find_element_by_id("com.android.keepass:id/pass_conf_password")
el.send_keys("00")

el = driver.find_element_by_id("com.android.keepass:id/ok")
el.click()

time.sleep(5)

# test case27: 当前群组下，锁定当前数据库，再次重新进入，然后退出
el = driver.find_element_by_id("com.android.keepass:id/menu_lock")
el.click()

time.sleep(1)

el = driver.find_element_by_id('com.android.keepass:id/open')
el.click()

el = driver.find_element_by_id("com.android.keepass:id/password")
el.send_keys("00")

el = driver.find_element_by_id("com.android.keepass:id/pass_ok")
el.click()

time.sleep(1)

driver.back()

time.sleep(1)

driver.back()

time.sleep(1)

driver.quit()






